package com.ruoyi.yljf.controller.AppController;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.yljf.domain.Course;
import com.ruoyi.yljf.domain.Drug;
import com.ruoyi.yljf.domain.Jingfang;
import com.ruoyi.yljf.domain.MedicalRecord;
import com.ruoyi.yljf.domain.vo.CollectCourseVO;
import com.ruoyi.yljf.domain.vo.DrugCollectVo;
import com.ruoyi.yljf.domain.vo.JingfangCollectVo;
import com.ruoyi.yljf.domain.vo.MedicalCollectVo;
import com.ruoyi.yljf.service.ICollectRecordService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 收藏记录Controller
 *
 * @author metaee
 * @date 2025-04-19
 */
@RestController
@RequestMapping("/api/collectRecord")
public class ApiCollectRecordController extends BaseController {
    @Resource
    private ICollectRecordService collectRecordService;

    @Resource
    private TokenService tokenService;


    /**
     * 收藏
     */
    @PostMapping(value = "/collect")
    public AjaxResult like(Long targetId, String targetType, HttpServletRequest req) {
        return success(collectRecordService.collect(targetType,
                targetId,
                tokenService.getLoginUser(req).getUserId()));
    }

    @GetMapping(value = "/course")
    public TableDataInfo course() {
        List<Course> collectCourseList = collectRecordService.getCollectCourseList(getUserId());
        List<CollectCourseVO> collectCourseVOS = BeanUtil.copyToList(collectCourseList, CollectCourseVO.class);
        return getDataTableInfo(collectCourseVOS, collectCourseList);
    }


    @GetMapping("/getJingfangRecordListByType")
    public TableDataInfo getJingfangRecordListByType() {
        List<Jingfang> objects = collectRecordService.selectJingfangRecordListByType(getUserId());
        if (!ObjectUtils.isNotEmpty(objects)) {
            return getDataTableInfo(objects, new ArrayList<JingfangCollectVo>());
        }
        List<JingfangCollectVo> collectVOS = BeanUtil.copyToList(objects, JingfangCollectVo.class);
        return getDataTableInfo(collectVOS, objects);
    }


    @GetMapping("/getMedicalRecordListByType")
    public TableDataInfo getMedicalRecordListByType() {
        List<MedicalRecord> objects = collectRecordService.selectMedicalRecordListByType(getUserId());


        if (!ObjectUtils.isNotEmpty(objects)) {
            return getDataTableInfo(objects, new ArrayList<MedicalCollectVo>());
        }
        List<MedicalCollectVo> collectVOS = BeanUtil.copyToList(objects, MedicalCollectVo.class);
        return getDataTableInfo(collectVOS, objects);
    }

    @GetMapping("/getDrugRecordListByType")
    public TableDataInfo getDrugRecordListByType() {
        List<Drug> objects = collectRecordService.selectDrugRecordListByType(getUserId());
        if (!ObjectUtils.isNotEmpty(objects)) {
            return getDataTableInfo(objects, new ArrayList<DrugCollectVo>());
        }
        List<DrugCollectVo> collectVOS = BeanUtil.copyToList(objects, DrugCollectVo.class);
        return getDataTableInfo(collectVOS, objects);
    }


}
